<template>
	<view class="">
		<image class='background' src="../../static/img/carbg1.jpg" mode="aspectFill"></image>
		<!-- <view class="tit">
			请选择你要申请的排班项目：
		</view> -->
		<view class="btn">
			<u-modal v-model="course3_res_show" :content="course3_res_cont"></u-modal>
			<u-modal v-model="show" @cancel="cancel" @confirm="confirm" show-cancel-button confirm-text="是" cancel-text="否" :content="content"></u-modal>
			<button class="lessonbtn" @click="handlecourse2(2)" >科目二</button>
			<button class="lessonbtn" @click="handlecourse3(3)">科目三</button>
		</view>
		<view class="wrap">
			<u-swiper :list="list" mode="dot"  @click="toSwiper()" indicator-pos="bottomRight" height="600"></u-swiper>
		</view>
</view>

</template>

<script>
	import api from "../../utils/api.js"
	export default {
		data() {
			return {
				user:{},
				show: false,
				course3_res_show:false,
				course:0,
				content:'是否已经预约考试',
				course3_res_cont:'成功申请科目三练车，请等待教练联系',
				list: [],
				imgs:[]
			}
		},
		created() {
			
			this.requestdata()
			this.getSwipper()
			this.getthreenews()
		},
		onShow() {
			this.getthreenews()
			this.requestdata()
			this.getSwipper()
		},
		methods:{
			requestdata(){
				api.getUserInfo(uni.getStorageSync('token')).then((res)=>{
						this.user = res.data.info
						uni.setStorageSync('userVO', res.data);
				})
			},
			getthreenews(){
				api.getunreadnews(uni.getStorageSync("user").branchId).then(res=>{
					if(res.data>0){
						wx.setTabBarBadge({
							index:2,
							text:res.data.toString()
						})
					}
				})
			},
			handlecourse2(id){
				if(this.user.branchId==null||this.user.email==null||this.user.phone==null||this.user.realName==null){
					this.course3_res_cont = '请先完善您的个人信息再申请。'
					this.course3_res_show = true
				}else{
				this.course = id;
				if(this.user.subject2Time>new Date().getTime()){
					wx.navigateTo({
						url:'/pages/apply/index'
					})
				}else{
					this.show = true;
				}
				}
				
			},
			handlecourse3(id){
				if(this.user.branchId==null||this.user.email==null||this.user.phone==null||this.user.realName==null){
					this.course3_res_cont = '请先完善您的个人信息再申请。'
					this.course3_res_show = true
				}else{
				this.course = id;
				if(this.user.subject3Time>new Date().getTime()){
					this.course3_res_cont = '您已成功预约科目三练车，请等待教练通知。'
					this.course3_res_show = true
				}else{
					this.show = true;
				}
				}
			},
			confirm(){
				if(this.course===2){
					api.getSubject2_3Status(this.course).then((res=>{
						if(res.code==0){
							this.requestdata()
						}
					}))
					api.postAnnounce({title:"已预约科目二考试",
					content:this.user.realName+'已预约科目二考试，请尽快安排练车',
					type:this.globalConst.COURSE_TWO_TYPE,}).then(res=>{
								console.log(res)
							})
					wx.navigateTo({
						url:'/pages/apply/index'
					})
				}else if(this.course===3){
					api.getSubject2_3Status(this.course).then((res=>{
						if(res.code==0){
					api.postAnnounce({title:"申请科目三练车",
					content:this.user.realName+'申请科目三练车,我的手机号码是：'+this.user.phone,
					type:this.globalConst.COURSE_THREE_TYPE,}).then(res=>{
								console.log(res)
							})
							this.requestdata()
							this.course3_res_cont='成功申请科目三练车，请等待教练联系'
							this.course3_res_show=true
						}
					}))
				}
			},
			cancel(){
				if(this.course===2){
					wx.navigateTo({
						url:'/pages/apply/index'
					})
				}
				if(this.course===3){
					this.course3_res_cont='请联系教练了解科目三站岗及考试注意事项，并预约考试'
					this.course3_res_show=true
				}
			},
			getSwipper(){
				let branchId = uni.getStorageSync("user").branchId
				console.log(branchId)
				console.log("-------------------------------")
				api.getRotationChartImageByBranchId(2).then(res => {
					let data = res.data
					console.log(data)
					this.list.splice(0, this.list.length)
					for(var i = 0;i < data.length;i++){
						var imgData ={
							image:''
						}
						imgData.image = this.globalConst.imageBaseUrl + data[i].image
						this.list.push(imgData)
					}
				})
				
				// api.getRotationChart(this.globalConst.APPOINTMENT_SWAPPER).then(res => {
				// 	let data = res.data
				// 	for(var i = 0;i < data.length;i++){
				// 		var imgData ={
				// 			image:''
				// 		}
				// 		imgData.image = this.globalConst.imageBaseUrl + data[i].image
				// 		this.list.push(imgData)
				// 	}
				// })
			},
			toSwiper(e){
				let imgs = this.list.map(item =>item.image)
				uni.previewImage({
					urls: imgs,
					current: e,
				})
			}
		}
	}
</script>

<style>
	.wrap {
		padding: 40rpx;
	}
	page{
	  height:100%;
	}
	.background {
	  width: 100%;
	  height: 100%;
	  position:fixed; 
	  background-size:100% 100%;
	  z-index: -1;
	}
	.tit{
		margin: 0 auto;
		padding-top: 30rpx;
		width: 86%;
	}
	.btn{
		display: flex;
		padding-top: 100rpx;
		margin: 0 auto;
		width: 92%;
	}
	.lessonbtn{
		width: 100%;
		height: 100%;
		line-height: 280rpx;
		color: black;
		background-color:rgba(87, 88, 176, 0.3);
		margin: 0 20rpx;
		font: 500 22px;
		transition: color 0.1s linear, background-color 0.1s linear, border-color 0.1s linear;
		-moz-border-radius: 5px;
		-webkit-border-radius: 5px;
		border-radius: 5px;
		-moz-box-sizing: border-box;
		-webkit-box-sizing: border-box;
		box-sizing: border-box;
	}
</style>